package com.jiangyg.mall.product.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.product.dto.AttrDTO;
import com.jiangyg.mall.product.vo.AttrVO;
import com.jiangyg.mall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：商品属性控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:38:50
 */
@RestController
@RequestMapping("/product/attr")
public class AttrController {

    /**
     * 商品属性接口
     */
    private final AttrService attrService;

    @Autowired
    public AttrController(AttrService attrService) {
        this.attrService = attrService;
    }

    /**
     * 功能描述：商品属性分页查询
     *
     * @param pagination 分页参数
     * @return 返回分页商品属性
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<AttrVO>> select(@RequestBody Pagination<AttrVO> pagination) throws Exception {
        return Result.ok(attrService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改商品属性
     *
     * @param request 请求
     * @param dto     商品属性
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated AttrDTO dto) throws Exception {
        return Result.saveOrUpdate(attrService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取商品属性
     *
     * @param request 请求
     * @param attrId  商品属性ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<AttrVO> info(HttpServletRequest request, @RequestParam(name = "attrId") Long attrId) throws Exception {
        return Result.ok(attrService.selectById(attrId));
    }

    /**
     * 功能描述：删除商品属性
     *
     * @param request 请求
     * @param attrIds 批量删除的商品属性ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> attrIds) {
        return Result.delete(attrService.delete(attrIds));
    }

}
